﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JobSiteStarterKit.ENUM;
using JobSiteStarterKit.BOL;

public partial class UserControls_employer_Jobs : System.Web.UI.UserControl
{
    private DBWork dbWork;

    public DBWork DbWork
    {
        get
        {
            return dbWork;
        }
        set
        {
            dbWork = value;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            drpCountry.DataSource = Country.GetCountries();
            drpCountry.DataTextField = "CountryName";
            drpCountry.DataValueField = "CountryID";
            drpCountry.DataBind();

            drpJobType.DataSource = JobType.GetJobTypes();
            drpJobType.DataTextField = "JobTypeName";
            drpJobType.DataValueField = "JobTypeID";
            drpJobType.DataBind();

            lstEducation.DataSource = EducationLevel.GetEducationLevels();
            lstEducation.DataTextField = "EducationLevelName";
            lstEducation.DataValueField = "EducationLevelID";
            lstEducation.DataBind();

            drpJobCategory.DataSource = Industry.GetIndustry();
            drpJobCategory.DataTextField = "CategoryName";
            drpJobCategory.DataValueField = "CategoryID";
            drpJobCategory.DataBind();

            drpJobFunctionalArea.DataSource = JobSiteStarterKit.BOL.FunctionalArea.GetAllFunctionalArea();
            drpJobFunctionalArea.DataTextField = "SubcategoryName";
            drpJobFunctionalArea.DataValueField = "SubcategoryID";
            drpJobFunctionalArea.DataBind();

            drpState.DataSource = JobSiteStarterKit.BOL.State.GetStates();
            drpState.DataTextField = "StateName";
            drpState.DataValueField = "StateID";
            drpState.DataBind();

            //set the company is to the viewstate of control
            int CompanyId = 0;
            JobSiteStarterKit.BOL.Company company = JobSiteStarterKit.BOL.Company.GetCompany(Context.User.Identity.Name);
          
            CompanyId = company.CompanyID;
            ViewState["Company_Id"] = CompanyId;
        }
    }
   
   
    public JobPosting InitializeJob()
    {
        JobPosting objJob = new JobPosting();
        objJob.Category = Convert.ToInt32(drpJobCategory.SelectedItem.Value);
        objJob.City = txtCity.Text.Trim();
        objJob.ClosingDate = Convert.ToDateTime( txtJobClosingDate.Text.Trim());
        //objJob.CompanyID = ;
        objJob.Compensation = txtCompensation.Text.Trim();
        objJob.ContactPerson = txtContactPerson.Text;
        objJob.CountryID = Convert.ToInt32(drpCountry.SelectedItem.Value);
        objJob.Department = drpJobCategory.SelectedItem.Text;
        objJob.Description = txtJobDesc.Text.Trim();
        string stredu = "";
        foreach (ListItem itm in lstEducation.Items)
        {
            if (itm.Selected == true)
            {
                stredu += itm.Value + ",";
            }
        }
        stredu = stredu.Substring(0, stredu.LastIndexOf(','));
        if (txtEducationOthers.Visible == true)
        {
            stredu += "," + txtEducationOthers.Text.Trim();
        }
        objJob.EducationalOthers = stredu;
        if(chkFreshers.Checked==true)
            objJob.EducationLevelID = Convert.ToInt32(chkFreshers.ToolTip);
        objJob.FunctionalArea = drpJobFunctionalArea.SelectedItem.Text;
        objJob.JobCode = txtJobCode.Text.Trim();
        objJob.JobTypeID = Convert.ToInt32(drpJobType.SelectedItem.Value);
        // try to convert the numeric value
        
        bool bln1,bln2;
        double num1, num2;
        bln1 = double.TryParse(txtMaxExpMonth.Text.Trim(), out num1);
        bln2 = double.TryParse(txtMaxExpYears.Text.Trim(), out num2);
        if(bln1==true)
               objJob.MaxExp = Convert.ToInt32(txtMaxExpMonth.Text.Trim());
        if (bln2)
        {
            objJob.MaxExp += Convert.ToInt32(txtMaxExpYears.Text.Trim()) * 12;
        }
            
        double maxSal;
        bln1 = double.TryParse(txtMaxSalary.Text.Trim(), out maxSal);

        if(bln1)
             objJob.MaxSalary = Convert.ToDecimal(txtMaxSalary.Text.Trim());
        double minExpMonth, minExpYear;
        bln1 = double.TryParse(txtMinExpMonth.Text.Trim(), out minExpMonth);
        bln2 = double.TryParse(txtMinExpYears.Text.Trim(), out minExpYear);
        if(bln1)
             objJob.MinExp = Convert.ToInt32(txtMinExpMonth.Text.Trim()) ;
        if(bln2)
            objJob.MinExp += Convert.ToInt32(txtMinExpYears.Text.Trim()) * 12 ;

        double minSal;
        bln1 = double.TryParse(txtMaxSalary.Text.Trim(), out minSal);
        if(bln1)
            objJob.MinSalary = Convert.ToDecimal(txtMinSalary.Text.Trim());

        objJob.OpeningDate = Convert.ToDateTime(txtJobOpeningDate.Text.Trim());
        objJob.StateID = Convert.ToInt32(drpState.SelectedItem.Value);
        objJob.Title = txtJobTitle.Text.Trim();
        if (txtContactPersonEmail.Text.Trim() != "")
            objJob.ContactPersonEmail = txtContactPersonEmail.Text.Trim();
        if (ViewState["Company_Id"] != null)
            objJob.CompanyID = Convert.ToInt32(ViewState["Company_Id"]);
        return objJob;

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (dbWork == DBWork.Insert)
        {
            JobPosting.Insert(InitializeJob());
            //redirect to the page that tells the job is posted successfully.
            // possibly my accounts.
            Response.Redirect("../Employer/myaccounts.aspx");
        }
        else if (dbWork == DBWork.Update)
            JobPosting.Update(InitializeJob());
        else if (dbWork == DBWork.Delete)
        {

        }
           
    }
    protected void drpCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        int CountryId = Convert.ToInt32(drpCountry.SelectedItem.Value);
        drpState.DataSource = State.GetStates(CountryId);
        drpState.DataTextField = "StateName";
        drpState.DataValueField = "StateID";
        drpState.DataBind();
    }
    protected void drpJobFunctionalArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpJobFunctionalArea.SelectedItem.Value == "-1")
        {
            txtOthersFunctionalArea.Visible = true;
        }
        else
        {
            txtOthersFunctionalArea.Visible = false;
        }
    }
   
    protected void lstEducation_SelectedIndexChanged1(object sender, EventArgs e)
    {
        if (lstEducation.SelectedItem != null)
        {
            if (lstEducation.SelectedItem.Value == "-1")
                txtEducationOthers.Visible = true;
            else
                txtEducationOthers.Visible = false;
        }
    }
    protected void drpJobCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        drpJobFunctionalArea.DataSource = FunctionalArea.GetFunctionalArea(Convert.ToInt32(drpJobCategory.SelectedItem.Value));
        drpJobFunctionalArea.DataTextField = "SubCategoryName";
        drpJobFunctionalArea.DataValueField = "SubCategoryId";
        drpJobFunctionalArea.DataBind();
    }
}
